//
//  GEEBaseViewController.m
//  KX11UI
//
//  Created by LJQ on 2021/2/19.
//

#import "ZLVH_GEEBaseViewController_HYC_Neusoft.h"
#import "NSString+Tools_HYC_Neusoft.h"
//#import "GEE_pushModel.h"
//#import "NSDate+HSSYDate.h"

static NSString *barKey1 = @"firstBarKey";
static NSString *barKey2 = @"secondBarkey";
static NSString *barKey3 = @"barKey3";



@interface ZLVH_GEEBaseViewController_HYC_Neusoft ()<UIGestureRecognizerDelegate>
@property (nonatomic ,strong) UIView* stateView;

@end

@implementation ZLVH_GEEBaseViewController_HYC_Neusoft

- (NSDictionary *)getTrackProperties {
    return @{
        @"$title": self.trackTitle ? : @""
    };
    
    
    
}
// 禁用返回手势 _________________________________________________________________________________________________HYC_________________________________________________________________________________
//
+ (void)popGestureClose:(UIViewController *)VC
{
    // 禁用侧滑返回手势
    if ([VC.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        //这里对添加到右滑视图上的所有手势禁用
        for (UIGestureRecognizer *popGesture in VC.navigationController.interactivePopGestureRecognizer.view.gestureRecognizers) {
            popGesture.enabled = NO;
        }
    }
}

+ (void)popGestureOpen:(UIViewController *)VC
{
    // 启用侧滑返回手势
    if ([VC.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        //这里对添加到右滑视图上的所有手势启用
        for (UIGestureRecognizer *popGesture in VC.navigationController.interactivePopGestureRecognizer.view.gestureRecognizers) {
            popGesture.enabled = YES;
        }
    }
}
//  _________________________________________________________________________________________________HYC_________________________________________________________________________________
//

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    
    if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.navigationController.interactivePopGestureRecognizer.delegate = self;
    }
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.navigationController.interactivePopGestureRecognizer.delegate = nil;
    }
    
}

//- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
//{
//    return NO;
//}




- (void)viewDidLoad {
    [super viewDidLoad];
    
    
//    id traget = self.navigationController.interactivePopGestureRecognizer.delegate;
//    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:traget action:nil];
//    [self.view addGestureRecognizer:pan];
    
    
    [UIViewController attemptRotationToDeviceOrientation];
    //  _________________________________________________________________________________________________HYC_________________________________________________________________________________
    //start
    self.navigationController.navigationBar.barStyle = UIStatusBarStyleDefault;
    
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 80000 // 当前Xcode支持iOS8及以上
        
    self.stateView = [[UIView alloc] initWithFrame:CGRectMake(
                                                              0,
                                                              
                                                              -(self.navigationController.navigationBar.frame.size.height + [[UIApplication sharedApplication] statusBarFrame].size.height),
                                                              
                                                              ([[UIScreen mainScreen] respondsToSelector:@selector(nativeBounds)] ? [UIScreen mainScreen].nativeBounds.size.width / [UIScreen mainScreen].nativeScale:[UIScreen mainScreen].bounds.size.width),
                                                              
                                                              (self.navigationController.navigationBar.frame.size.height + [[UIApplication sharedApplication] statusBarFrame].size.height)
                                                              )];
#else
        
    self.stateView = [[UIView alloc] initWithFrame:CGRectMake(
                                                              0,
                                                              
                                                              -(self.navigationController.navigationBar.frame.size.height + [[UIApplication sharedApplication] statusBarFrame].size.height),
                                                              
                                                              [UIScreen mainScreen].bounds.size.width,
                                                              
                                                              (self.navigationController.navigationBar.frame.size.height + [[UIApplication sharedApplication] statusBarFrame].size.height)
                                                              )];
#endif
    
    
    
    _stateView.backgroundColor = UIColor.whiteColor;
    
    if (!_stateView) {
        [self.navigationController.navigationBar addSubview:_stateView];
    }
    [self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc]init] forBarMetrics:UIBarMetricsDefault];
    
    
    
    //  _________________________________________________________________________________________________HYC_________________________________________________________________________________
    //Over
    
    self.view.backgroundColor = UIColor.whiteColor;
    
//    if (@available(iOS 11.0 , *)) {
//        UITableView.appearance.estimatedRowHeight = 0;
//        UITableView.appearance.estimatedSectionFooterHeight = 0;
//        UITableView.appearance.estimatedSectionHeaderHeight = 0;
//        [[UITabBar appearance] setTranslucent:NO];
//    }else {
//        self.automaticallyAdjustsScrollViewInsets = false;
//    }
//    [[UITabBar appearance] setTranslucent:NO];
    self.extendedLayoutIncludesOpaqueBars = true;
    self.edgesForExtendedLayout = UIRectEdgeNone;
//    [self isPushVC];
    // Do any additional setup after loading the view.
    
    
    
    
    
    
    //返回按钮
    UIImageView *leftBackImage = UIImageView.alloc.init;
    leftBackImage.image = @"ZLVH_nav_back_HYC".image;
    
    
    // 弱引用
//    __weak typeof(self) weakSelf = self;

    UITapGestureRecognizer * tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(leftBtnClick)];
    
    //设置需要连续点击几次才响应，默认点击1次
    [tapGesture setNumberOfTapsRequired:1];
    
    [leftBackImage addGestureRecognizer:tapGesture];
    
    [self setLeftView:leftBackImage];
    
}


- leftBtnClick {
    
    [self pop];

    return self;
}




- (id)jSONSerializationWithResponseObject:(id)responseObject{
    if (responseObject == nil) {
        return nil;
    }
    
    id json = nil;
    //  判断数据为json
    if ([responseObject isKindOfClass:[NSArray class]] ||
        [responseObject isKindOfClass:[NSDictionary class]] ||
        [responseObject isKindOfClass:[NSString class]])
    {
        json = responseObject;
    }// 不为json（NSdata）解析
    else {
        NSError *json_err = nil;
        json = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:&json_err];
    }
    return json;
}







-(void)addDataToPlist:(NSMutableDictionary*)dic
{
    // path
    NSArray  *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentDirectory = [paths objectAtIndex:0];
    NSString *fileName = [NSString stringWithFormat:@"GEEKXMsgBox.plist"];
    NSString *logFilePath = [documentDirectory stringByAppendingPathComponent:fileName];
    
    // read
    NSMutableDictionary *dataDictionary = [[NSMutableDictionary alloc] initWithContentsOfFile:logFilePath];
    NSLog(@"---plist一开始保存时候的内容---%@",dataDictionary);
    
    // 判空
    NSMutableArray *dataArrtest = [[NSMutableArray alloc] init];
    if (dataDictionary.count != 0 && dataDictionary != nil &&![dataDictionary isKindOfClass:[NSNull class]] )
    {
        dataArrtest = [dataDictionary objectForKey:@"data"];
        NSLog(@"---plist一开始保存时候的数组---%@",dataArrtest);
        int arrayCount = (int)dataArrtest.count;
        
        if(arrayCount>10000)// 判断数量是否大于10000
        {
            //不再追加
            return;
        }
    }

    // single data dic 本次存储
    NSMutableDictionary *dataSigleDic = [[NSMutableDictionary alloc]initWithDictionary:dic];
                                   
    // add data array
    [dataArrtest addObject:dataSigleDic];
    
    // All Dic
    NSMutableDictionary *allDic = [[NSMutableDictionary alloc]initWithCapacity:0];
    [allDic setObject:@"body" forKey:@"type"];
    [allDic setObject:dataArrtest forKey:@"data"];
    
    // write to file
    [allDic writeToFile:logFilePath atomically:YES];
}


- (BOOL)shouldAutorotate
{
    return NO;
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}



//#pragma mark - 页面跳转
//-(void)isPushVC {
//    ZLVH_GEE_WeakSelf(weakSelf);
//    UIImageView *backImg = [[UIImageView alloc] init];
//    backImg.image = GEE_IMAGE_NAMED(@"back");
//    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithCustomView:backImg];
//    NSArray *viewControlles = self.navigationController.viewControllers;
//    if (viewControlles.count > 1) {
//        if ([viewControlles objectAtIndex:viewControlles.count - 1] == self) {
//            self.navigationItem.leftBarButtonItem = backItem;
//            [backImg addGuest:tapAction resutlBlock:^(UITapGestureRecognizer * _Nonnull gesture) {
//                [weakSelf pop];
//            }];
//        }
//    }
//}


/// POP到指定页面
/// @param vc vc description
-(void) popTo:(UIViewController *)vc {
    for (UIViewController *item in self.navigationController.viewControllers) {
        if ([item isKindOfClass:vc.class]) {
            [self.navigationController popToViewController:item animated:true];
        }else {
            NSLog(@"未找到页面");
        }
    }
}

/// 返回
-(void)pop {
    [self.navigationController popViewControllerAnimated:true];
}

/// 跳转
/// @param vc vc description
-(void)push:(UIViewController *)vc {
    [self.navigationController pushViewController:vc animated:true];
}

/// 跳转是否隐藏Tabbar
/// @param vc vc description
/// @param hidden hidden description
-(void)push:(UIViewController *)vc isBottomHidden:(BOOL)hidden {
//    [self.navigationController setHidesBottomBarWhenPushed:hidden];
    [self setHidesBottomBarWhenPushed:hidden];
    [self push:vc];
    [self setHidesBottomBarWhenPushed:!hidden];
}


///// rightBarBtns
///// @param first first description
///// @param firstBlock firstBlock description
///// @param second second description
///// @param secondBlock secondBlock description
//-(NSArray<UIBarButtonItem *> *)setRightItems:(NSString *)first firstBlock:(BarBtnBlock)firstBlock second:(NSString *)second second:(BarBtnBlock)secondBlock {
//    UIImage *img1 = first.image;
//    UIImage *img2 = first.image;
//    img1 = [img1 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
//    img2 = [img2 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
//    UIBarButtonItem *barBtn1 = [[UIBarButtonItem alloc] init];
//    UIBarButtonItem *barBtn2 = [[UIBarButtonItem alloc] init];
//
//    objc_setAssociatedObject(self, &barKey1, firstBlock, OBJC_ASSOCIATION_COPY);
//    objc_setAssociatedObject(self, &barKey2, secondBlock, OBJC_ASSOCIATION_COPY);
//
//    if (img1) {
//        [barBtn1 setImage:img1];
//    }else {
//        [barBtn1 setTitle:first];
//    }
//    [barBtn1 setTarget:self];
//    [barBtn1 setAction:@selector(barBtnCallBack1:)];
//
//    if (img2) {
//        [barBtn2 setImage:img2];
//    }else {
//        [barBtn2 setTitle:second];
//    }
//    [barBtn2 setTarget:self];
//    [barBtn2 setAction:@selector(barBtnCallBack2:)];
//
//    self.navigationItem.rightBarButtonItems = @[barBtn1,barBtn2];
//    return self.navigationItem.rightBarButtonItems;
//}
//
//-(void)barBtnCallBack1:(BarBtnBlock)block {
//    BarBtnBlock guestBlock1 = objc_getAssociatedObject(self, &barKey1);
//    if (guestBlock1) {
//        guestBlock1();
//    }
//}
//
//-(void)barBtnCallBack2:(BarBtnBlock)block {
//    BarBtnBlock guestBlock2 = objc_getAssociatedObject(self, &barKey2);
//    if (guestBlock2) {
//        guestBlock2();
//    }
//}





/// navigationBar leftItem
/// @param itemView 左侧标题
-(void)setLeftView:(UIView *)itemView {
    [itemView setUserInteractionEnabled:YES];
    UIBarButtonItem *barBtn1 = [[UIBarButtonItem alloc] initWithCustomView:itemView];
    self.navigationItem.leftBarButtonItems = @[barBtn1];
}

@end
